﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Question
namespace backoffice.Data
{
    partial class Question
    {
        public void setQuestion(Question q)
        {
            this.QuestionID = q.QuestionID;
            this.UserID = q.UserID; 
            this.PathologyID = q.PathologyID;
            this.PlantID = q.PlantID;
            this.MethodID = q.MethodID;
            this.Date = q.Date;
            this.Text = q.Text;
        }
    }
}
#endregion

#region dataQuestion
namespace backoffice.Data
{
    public class dataQuestion
    {
        #region Create
        public static void createQuestion(Question q)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                q.QuestionID = Guid.NewGuid();
                q.Date = DateTime.Now;

                db.Questions.InsertOnSubmit(q);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Question getQuestion(Guid guid)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                Question question = (from q in db.Questions
                                     where q.QuestionID == guid
                                     select q).FirstOrDefault();

                return question;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Question getQuestion(Expression<Func<Question, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Questions.Where(whereClause).FirstOrDefault();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Question> getAllQuestions()
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Questions;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Question> getQuestions(Expression<Func<Question, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Questions.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        #endregion

        #region Set
        public static Boolean changeQuestion(Question q)
        {
            try
            {
                if (q == null) return false;
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Question res = db.Questions.FirstOrDefault(ques => ques.QuestionID == q.QuestionID);
                    if (res == null) return false;
                    else
                    {
                        res.setQuestion(q);
                        db.SubmitChanges();
                        return true;
                    }
                }

            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteQuestion(Question q)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Questions.Contains(q))
                {
                    Question res = db.Questions.FirstOrDefault(question => question.QuestionID == q.QuestionID);

                    if (res == null) return false;
                    else
                    {
                        // Delete answers
                        IQueryable<Answer> answers = db.Answers.Where((Answer an) => (an.QuestionID == res.QuestionID));
                        db.Answers.DeleteAllOnSubmit(answers);
                        
                        // Delete question
                        db.Questions.DeleteOnSubmit(res);
                        db.SubmitChanges();
                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
#endregion